package stephencarmody.random_rotating_triangles;

import stephencarmody.k8.core.Logic;
import stephencarmody.k8.core.Application;
import stephencarmody.k8.core.TexturedTriangles;
import stephencarmody.k8.core.Triangle;
import stephencarmody.k8.text.Text;

public class FPSCounter extends Logic {
    
    private Text text;
    private long lastupdate;
    private long elapsed;
    private float secs;
    private float lastfps;
    private int fps;
    private Triangle t1, t2;
    private int x;
    
    /** Creates a new instance of FPSCounter */
    public FPSCounter() {
    	setUpdateRate(1);
    	text = new Text();
    	x = -100;
    }
    
    public void update(long time) {
        // Calculate elapsed time
        elapsed = time - lastupdate;
        lastupdate = time;
        
        // Update Text
        secs = (float) elapsed / 1000000000;
        fps = Math.round((float)Application.frame()/secs);
        text.setText(TexturedTriangles.count + " @ " + fps + " fps");
        
        if ( fps != lastfps ) {
        	t1 = new Triangle();
            t1.texture(Main.wood);
            t1.vertices(0, fps, 0, 0, 0, 0, 1, fps, 0);
            t1.texCoords(0,0,0,1,1,0);
            t2 = new Triangle();
            t2.texture(Main.wood);
            t2.vertices(0, 0, 0, 1, 0, 0, 1, fps, 0);
            t2.texCoords(0,1,1,1,1,0);
            t1.appendChild(t2);
            t1.moveRight(x);
            t1.moveForward(100);
            x++;
        }
        
        lastfps = fps;
    }
    
    public Text getText() {
    	return text;
    }
    
}
